package com.qili.service.supplier.impl;

import cn.hutool.core.util.IdUtil;
import com.qili.base.mapper.BaseMapper;
import com.qili.base.service.impl.BaseServiceImpl;
import com.qili.entity.base.BaseDict;
import com.qili.entity.supervisor.dto.SupervisorProjectDTO;
import com.qili.entity.supervisor.dto.SupervisorProjectEvaluationDTO;
import com.qili.entity.supplier.RecommendReason;
import com.qili.entity.supplier.SupplierProject;
import com.qili.entity.supplier.SupplierProjectAccept;
import com.qili.entity.supplier.dto.SupplierProjectAcceptDTO;
import com.qili.entity.sys.SysUser;
import com.qili.entity.sys.SysUserCertificate;
import com.qili.entity.sys.SysUserWorkExperience;
import com.qili.mapper.base.BaseDictMapper;
import com.qili.mapper.supervisor.SupervisorProjectMapper;
import com.qili.mapper.supplier.RecommendReasonMapper;
import com.qili.mapper.supplier.SupplierProjectAcceptMapper;
import com.qili.mapper.supplier.SupplierProjectMapper;
import com.qili.mapper.sys.SysUserCertificateMapper;
import com.qili.mapper.sys.SysUserMapper;
import com.qili.mapper.sys.SysUserProjectMapper;
import com.qili.mapper.sys.SysUserWorkExperienceMapper;
import com.qili.service.supplier.RecommendReasonService;
import com.qili.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zhangxiaopeng
 * @date 2021-04-10
 */
@Service
public class RecommendReasonServiceImpl extends BaseServiceImpl<RecommendReason,String> implements RecommendReasonService {

    @Autowired
    private RecommendReasonMapper recommendReasonMapper;
    @Autowired
    private SupervisorProjectMapper supervisorProjectMapper;
    @Autowired
    private SysUserProjectMapper sysUserProjectMapper;
    @Autowired
    private SupplierProjectAcceptMapper supplierProjectAcceptMapper;
    @Autowired
    private SupplierProjectMapper supplierProjectMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysUserCertificateMapper sysUserCertificateMapper;
    @Autowired
    private BaseDictMapper baseDictMapper;
    @Autowired
    private SysUserWorkExperienceMapper sysUserWorkExperienceMapper;
    @Override
    public BaseMapper<RecommendReason, String> getMappser() {
        return recommendReasonMapper;
    }

    //查询综合对比
    public List<RecommendReason> selectReason(Map map){
        return recommendReasonMapper.selectReason(map);
    }
    //传入项目表的id 综合对比
    public void insertComparison(String id){
        SupplierProjectAccept supa=new SupplierProjectAccept();
        supa.setSupProjectId(id);

        SupplierProjectAcceptDTO spaDTO=new SupplierProjectAcceptDTO();
        RecommendReason reason=new RecommendReason();
        Date currdate = new Date();
        SupplierProject sup= supplierProjectMapper.selectByPrimaryKey(id);
        if(sup!=null&&!"".equals(sup)){
            reason.setProjectId(id);
            //判断该项目是否已生成
            List<RecommendReason> reasonList= recommendReasonMapper.select(reason);
            if(reasonList==null||reasonList.size()==0) {
                Date zbEndDate = DateUtil.parseDate(sup.getZbEndDate(), "yyyy-MM-dd HH:mm");
                if (currdate.after(zbEndDate)) {
                    //根据项目查询应接人的数据
                    List<SupplierProjectAccept> list = supplierProjectAcceptMapper.select(supa);
                    if (list != null && list.size() > 0) {
                        for (SupplierProjectAccept s : list) {
                            insertReason(s);
                        }
                    }
                    spaDTO.setSupProjectId(id);
                    spaDTO.setScoreDesc("111");
                    //根据应接人的总分数来进行排序
                    updateAccept(spaDTO);
                    //平台推荐顺序
                    updateReason(id);
                }
            }
        }


    }
    //根据应接人的总分数来进行排序
    public void updateAccept(SupplierProjectAcceptDTO spaDTO){
        List<SupplierProjectAcceptDTO> list= supplierProjectAcceptMapper.selectListByScore(spaDTO);
        if(list!=null&& list.size()>3){
            int a=1;
            for(int i=0;i<3;i++){
                list.get(i).setAcceptOrder(a+"");
                a++;
                supplierProjectAcceptMapper.updateByPrimaryKeySelective(list.get(i));
            }
        }else{
            int a=1;
            for(int i=0;i<list.size();i++){
                list.get(i).setAcceptOrder(a+"");
                a++;
                supplierProjectAcceptMapper.updateByPrimaryKeySelective(list.get(i));
            }
        }
    }
    //平台推荐顺序
    public void updateReason(String id){
        Map map=new HashMap<>();
        map.put("projectId",id);
        map.put("scoreDesc","123");
        List<RecommendReason> list= recommendReasonMapper.selectReason(map);
        if(list!=null&& list.size()>0){
            int a=1;
            for(RecommendReason re:list){
                re.setRecommendOrder(a+"");
                a++;
                recommendReasonMapper.updateByPrimaryKeySelective(re);
            }
        }
    }
    //传入应接表的id 综合对比
    public void insertReason(SupplierProjectAccept supplierProjectAccept){
        //查询应接单位数据
        SupplierProjectAccept project = supplierProjectAcceptMapper.selectOne(supplierProjectAccept);
        //查询应接人学历
        SysUser sysUser=new SysUser();
        sysUser.setId(project.getAcceptBy());
        SysUser user=sysUserMapper.selectOne(sysUser);
        //综合对比表新增
        RecommendReason reason=new RecommendReason();
        reason.setId(IdUtil.simpleUUID());
        reason.setProjectId(project.getSupProjectId());
        reason.setAcceptId(project.getSupAcceptId());
        reason.setComType(project.getComType());
        reason.setCompanyId(project.getCompanyId());
        //学历
        String education=baseDict("XL001",user.getEducation()).getBadtName();
        reason.setEducation(education);
        //项目个数
        //总监理工程师、咨询顾问  type:历史项目承担角色 status:项目完成状态
         String zzProjects=selectProjects(project.getAcceptBy(),"002",null);
         reason.setZzProjects(zzProjects);
        //项目经理
         String jlProjects=selectProjects(project.getAcceptBy(),"003",null);
        reason.setJlProjects(jlProjects);
        //助力、工程师
        String zgProjects=selectProjects(project.getAcceptBy(),"004",null);
        reason.setZgProjects(zgProjects);
        //项目个数得分
        int xmgs=Integer.parseInt(zzProjects)+Integer.parseInt(jlProjects)+Integer.parseInt(zgProjects);

        //符合发布需求中人员要求的擅长领域的项目个数
        //总监理工程师、咨询顾问  type:历史项目承担角色 status:项目完成状态
        String adeptZzProjects= adetProjects(user,"002",null);
        reason.setAdeptZzProjects(adeptZzProjects);
        //项目经理
        String adeptJProjects=adetProjects(user,"003",null);
        reason.setAdeptJProjects(adeptJProjects);
        //助力、工程师
        String adeptZgProjects=adetProjects(user,"004",null);
        reason.setAdeptZgProjects(adeptZgProjects);
        //删除领域项目个数得分
        int scxmgs=Integer.parseInt(adeptZzProjects)+Integer.parseInt(adeptJProjects)+Integer.parseInt(adeptZgProjects);

        //单个项目最大投资额
        //角色为总监理工程师/咨询顾问时   type:历史项目承担角色 status:项目完成状态
        String zzMaxMoney =maxMoney(project.getAcceptBy(),"002",null);
        reason.setZzMaxMoney(zzMaxMoney);
        //角色为项目经理时
        String jlMaxMoney =maxMoney(project.getAcceptBy(),"003",null);
        reason.setJlMaxMoney(jlMaxMoney);
        //角色为助理/工程师时
        String zgMaxMoney =maxMoney(project.getAcceptBy(),"004",null);
        reason.setZgMaxMoney(zgMaxMoney);
        //最大投资金额得分
        int zdtze=Integer.parseInt(moneyScore("ZZZDTZE001",zzMaxMoney))+Integer.parseInt(moneyScore("JLZDTZE001",jlMaxMoney))+Integer.parseInt(moneyScore("ZGZDTZE001",zgMaxMoney));

        //所有项目投资总额
        //角色为总监理工程师/咨询顾问时   type:历史项目承担角色 status:项目完成状态
        String zzSumMoney =sumMoney(project.getAcceptBy(),"002",null);
        reason.setZzSumMoney(zzSumMoney);
        //角色为项目经理时
        String jlSumMoney =sumMoney(project.getAcceptBy(),"003",null);
        reason.setJlSumMoney(jlSumMoney);
        //角色为助理/工程师时
        String zgSumMoney =sumMoney(project.getAcceptBy(),"004",null);
        reason.setZgSumMoney(zgSumMoney);
        //投资总金额得分
        int tzze=Integer.parseInt(moneyScore("ZZTZZE001",zzSumMoney))+Integer.parseInt(moneyScore("JLTZZE001",jlSumMoney))+Integer.parseInt(moneyScore("ZGTZZE001",zgSumMoney));

        //单个项目最大服务费额/服务合同金额
        //角色为总监理工程师/咨询顾问时
        String zzMaxCharge =maxCharge(project.getAcceptBy(),"002",null);
        reason.setZzMaxCharge(zzMaxCharge);
        //角色为项目经理时
        String jlMaxCharge =maxCharge(project.getAcceptBy(),"003",null);
        reason.setJlMaxCharge(jlMaxCharge);
        //角色为助理/工程师时
        String zgMaxCharge =maxCharge(project.getAcceptBy(),"004",null);
        reason.setZgMaxCharge(zgMaxCharge);
        //服务金额得分
        int zdfwe=Integer.parseInt(moneyScore("ZZZDJLF001",zzMaxCharge))+Integer.parseInt(moneyScore("JLZDJLF001",jlMaxCharge))+Integer.parseInt(moneyScore("ZGZDJLF001",zgMaxCharge));

        //所有项目服务费额/服务合同金额总额
        //角色为总监理工程师/咨询顾问时
        String zzSumCharge =sumCharge(project.getAcceptBy(),"002",null);
        reason.setZzSumCharge(zzSumCharge);
        //角色为项目经理时
        String jlSumCharge =sumCharge(project.getAcceptBy(),"003",null);
        reason.setJlSumCharge(jlSumCharge);
        //角色为助理/工程师时
        String zgSumCharge =sumCharge(project.getAcceptBy(),"004",null);
        reason.setZgSumCharge(zgSumCharge);
        //服务费总金额得分
        int fwze=Integer.parseInt(moneyScore("ZZJLZE001",zzSumCharge))+Integer.parseInt(moneyScore("JLJLZE001",jlSumCharge))+Integer.parseInt(moneyScore("ZGJLZE001",zgSumCharge));

        //项目服务费额/服务合同金额50w及以上的项目个数
        //角色为总监理工程师/咨询顾问时
        String zzGreaterCharge =chargeProjects(project.getAcceptBy(),"002",null);
        reason.setZzGreaterCharge(zzGreaterCharge);
        //角色为项目经理时
        String jlGreaterCharge =chargeProjects(project.getAcceptBy(),"003",null);
        reason.setJlGreaterCharge(jlGreaterCharge);
        //角色为助理/工程师时
        String zgGreaterCharge =chargeProjects(project.getAcceptBy(),"004",null);
        reason.setZgGreaterCharge(zgGreaterCharge);
        //服务费在50w及以上的项目个数得分
        int xmgsws=Integer.parseInt(zzGreaterCharge)+Integer.parseInt(jlGreaterCharge)+Integer.parseInt(zgGreaterCharge);

        //项目投资额在10000w及以上的项目个数
        //角色为总监理工程师/咨询顾问时   type:历史项目承担角色 status:项目完成状态
        String zzGreaterMoney =moneyProjects(project.getAcceptBy(),"002",null);
        reason.setZzGreaterMoney(zzGreaterMoney);
        //角色为项目经理时
        String jlGreaterMoney =moneyProjects(project.getAcceptBy(),"003",null);
        reason.setJlGreaterMoney(jlGreaterMoney);
        //角色为助理/工程师时
        String zgGreaterMoney =moneyProjects(project.getAcceptBy(),"004",null);
        reason.setZgGreaterMoney(zgGreaterMoney);
        //项目投资额在10000w及以上的项目个数得分
        int xmgsyw=Integer.parseInt(zzGreaterMoney)+Integer.parseInt(jlGreaterMoney)+Integer.parseInt(zgGreaterMoney);

        //岗位职级
        SysUserWorkExperience suwe= new SysUserWorkExperience();
        suwe.setSuId(project.getAcceptBy());
        //查询工作状况
        SysUserWorkExperience sys= sysUserWorkExperienceMapper.selectByPrimaryKey(suwe);
        String jobGrade="";
        if(sys!=null&&!"".equals(sys)){
            jobGrade=baseDict("GWZC001",sys.getStationProfessor()).getBadtName();
        }
        reason.setJobGrade(jobGrade);

        //相关资质
        SysUserCertificate scf=new SysUserCertificate();
        scf.setSuId(user.getId());
        List<SysUserCertificate> list=sysUserCertificateMapper.select(scf);
        int certificateNumber=0;
        String certificateName="";
        String certificateRank="";
        String name="";
        String rank="";
        if(list!=null&&list.size()>0){
            for(SysUserCertificate s:list){
                if(StringUtils.isNotBlank(s.getCertificateName())){
                    BaseDict baseDict=baseDict("ZSMC001",s.getCertificateName());
                    if(baseDict!=null&&!"".equals(baseDict)){
                        name=baseDict.getBadtName();
                    }
                }
                if(StringUtils.isNotBlank(name)){
                    certificateName+=name+",";
                    ++certificateNumber;
                }
                if(StringUtils.isNotBlank(s.getCertificateName())){
                    BaseDict baseDict=baseDict("ZSJB001",s.getCertificateLevel());
                    if(baseDict!=null&&!"".equals(baseDict)){
                        rank=baseDict.getBadtName();
                    }
                }
                if(StringUtils.isNotBlank(rank)){
                    certificateRank+=rank+",";
                }
            }
            if(certificateName.length()>0){
                certificateName=certificateName.substring(0,certificateName.length()-1);
            }
            if(certificateRank.length()>0){
                certificateRank=certificateRank.substring(0,certificateRank.length()-1);
            }

        }

        //证书名称
        reason.setCertificateName(certificateName);
        //证书个数
        reason.setCertificateNumber(certificateNumber+"");
        //证书级别
        reason.setCertificateRank(certificateRank);

        //平台项目实施
        //在平台中已承接项目个数
        String platformProjects=selectProjects(project.getAcceptBy(),null,"000");
        reason.setPlatformProjects(platformProjects);
        //符合发布需求中人员要求的擅长领域的项目个数
        String skilledProjects=adetProjects(user,null,"000");
        reason.setSkilledProjects(skilledProjects);
        //单个项目最大投资额
        String maxMoney=maxMoney(project.getAcceptBy(),null,"000");
        reason.setMaxMoney(maxMoney);
        //项目投资总额
        String sumMoney=sumMoney(project.getAcceptBy(),null,"000");
        reason.setSumMoney(sumMoney);
        //单个项目最大服务费
        String maxCharge=maxCharge(project.getAcceptBy(),null,"000");
        reason.setMaxCharge(maxCharge);
        //项目服务费总额
        String sumCharge=sumCharge(project.getAcceptBy(),null,"000");
        reason.setSumCharge(sumCharge);
        //服务费额50w及以上的项目个数
        String greaterCharge=chargeProjects(project.getAcceptBy(),null,"000");
        reason.setGreaterCharge(greaterCharge);
        //投资额10000w及以上的项目个数
        String greaterMoney=moneyProjects(project.getAcceptBy(),null,"000");
        reason.setGreaterMoney(greaterMoney);
        //项目工期
         String projectDuration=projectDuration(project.getAcceptBy());
        reason.setProjectDuration(projectDuration);
        //是否续签
        String renew=renew(project.getAcceptBy());
        reason.setRenew(renew);
        //回款及时性
        String easeBack=easeBack(project.getAcceptBy());
        reason.setEaseBack(easeBack);
        //束后总结及时性
        String summarize=summarize(project.getAcceptBy());
        reason.setSummarize(summarize);
        //平台承接项目个数得分
        int ptxmgs=Integer.parseInt(platformProjects)+Integer.parseInt(skilledProjects)+Integer.parseInt(greaterCharge)+Integer.parseInt(greaterMoney);
        //平台承接项目金额得分
        int ptxmje=Integer.parseInt(moneyScore("ZDTZE001",maxMoney))+Integer.parseInt(moneyScore("TZZE001",sumMoney))+
                   Integer.parseInt(moneyScore("ZDFWE001",maxCharge))+Integer.parseInt(moneyScore("FWZE001",sumCharge));

        //平台评价指标
        //服务质量
        String  serveQuality=evaluate(project.getAcceptBy(),"pjofxsdbfwzl");
        reason.setServeQuality(serveQuality);
        //技术能力
        String  technicalCapacity=evaluate(project.getAcceptBy(),"pjofssryjsnl");
        reason.setServeQuality(technicalCapacity);
        //项目把控能力
        String  projectHandle=evaluate(project.getAcceptBy(),"pjofssryglnl");
        reason.setServeQuality(projectHandle);
        //沟通协作能力
        String  coordinateCapacity=evaluate(project.getAcceptBy(),"pjofgtxtnl");
        reason.setServeQuality(coordinateCapacity);
        //工作主动性和责任心
        String  initiative=evaluate(project.getAcceptBy(),"pjofgzzdxzrx");
        reason.setServeQuality(initiative);
        //应接顺序
        SupplierProjectAccept reasons=new SupplierProjectAccept();
        reasons.setSupProjectId(project.getSupProjectId());
        List<SupplierProjectAccept> list1= recommendReasonMapper.selectSupList(reasons);
        int order=0;
        if(list1!=null&&list1.size()>0){
            for(SupplierProjectAccept sup:list1){
                ++order;
                if(sup.getSupAcceptId().equals(project.getSupAcceptId())){
                    break;
                }
            }
        }
        reason.setAcceptOrder(order+"");
        //创建日期
        reason.setCreateDate(DateUtil.getCurrentDateTimeString());
        //应接顺序得分
        int yjsxScore=list1.size()-order+1;
        //项目个数总得分
        int projectScore=0;
        projectScore=xmgs+scxmgs+xmgsws+xmgsyw+ptxmgs;
        //金额总得分
        int moneyScore=0;
        moneyScore=ptxmje+fwze+zdfwe+tzze+zdtze;
        //学历得分
        int xlScore=Integer.parseInt(score("XLFZ001",user.getEducation()));
        //岗位职级得分
        int zjScore=0;
        if(sys!=null&&!"".equals(sys)){
            Integer.parseInt(score("GWZJ001",sys.getStationProfessor()));
        }
        //项目工期得分
        int xmgqScore=durationScore(project.getAcceptBy());
        //是否续签得分
        int sfxqScore=renewScore(project.getAcceptBy());
        //回款及时性得分
        int hkjsScore=easeBackScore(project.getAcceptBy());
        //束后总结及时性得分
        int shzjScore=summarizeScore(project.getAcceptBy());
        //服务质量得分
        int fwzlScore=Integer.parseInt(moneyScore("FWZL001",serveQuality));
        //技术能力得分
        int jsnlScore=Integer.parseInt(moneyScore("JSNL001",technicalCapacity));
        //项目把控能力得分
        int bknlScore=Integer.parseInt(moneyScore("XMBKNL001",projectHandle));
        //沟通协作能力得分
        int xznlScore=Integer.parseInt(moneyScore("XTNL001",coordinateCapacity));
        //工作主动性和责任心得分
        int zrxScore=Integer.parseInt(moneyScore("ZRX001",initiative));
        //证书个数得分
        int zsgsScore=Integer.parseInt(certificateNumber+"");
        //证书名称得分
        int zsmcScore=competency(scf,"ZSMC001");
        //证书级别得分
        int zsjbScore=competency(scf,"ZSJB001");
        String score="";
        score="项目个数总得分:"+projectScore+";金额总得分:"+moneyScore+";学历得分:"+xlScore+";岗位职级得分:"+zjScore+";项目工期得分:"+xmgqScore+";是否续签得分:"+sfxqScore+
        ";回款及时性得分:"+hkjsScore+";束后总结及时性得分:"+shzjScore+";服务质量得分:"+fwzlScore+";技术能力得分"+jsnlScore+";项目把控能力得分:"+bknlScore+";沟通协作能力得分:"+xznlScore+
        ";工作主动性和责任心得分:"+zrxScore+";证书个数得分:"+zsgsScore+";证书名称得分:"+zsmcScore+";证书级别得分:"+zsjbScore+";应接顺序得分："+yjsxScore;
        int sumScore=0;
        sumScore=projectScore+moneyScore+xlScore+zjScore+xmgqScore+sfxqScore+hkjsScore+shzjScore+fwzlScore+jsnlScore+bknlScore+xznlScore+zrxScore+zsgsScore+zsmcScore+zsjbScore+yjsxScore;
        reason.setSumScore(sumScore+"");
        reason.setScore(score);
        SupplierProjectAccept supa=new SupplierProjectAccept();
        supa.setSupAcceptId(project.getSupAcceptId());
        supa.setScore(sumScore+"");
        supplierProjectAcceptMapper.updateByPrimaryKeySelective(supa);
        recommendReasonMapper.insertSelective(reason);
    }
    //传入字典类型编号，字典编号 查出字典名
    public BaseDict baseDict(String type,String code){
        BaseDict base=new BaseDict();
        base.setBadtTypecode(type);
        base.setBadtCode(code);
        BaseDict baseDict =new BaseDict();
        if(StringUtils.isNotBlank(code)) {
            baseDict = baseDictMapper.selectOne(base);
        }
        return baseDict;
    }

    //项目个数   type有值是查询监理项目否则是个人中心
    public String selectProjects(String id,String type,String status){
        Map map=new HashMap();
        SupervisorProjectDTO dto=new SupervisorProjectDTO();
        dto.setStatus(status);
        int projectNum=0;
        map.put("userId",id);
        if("002".equals(type)){
            //总监理工程师
            map.put("supRole","002");
            map.put("supRoles","007");
            dto.setSupervisorEngineer(id);
        }else if("003".equals(type)) {
            //项目经理
            map.put("supRole","002");
            dto.setProjectManagerPerson(id);
        }else if("004".equals(type)){
            map.put("supRole","004");
            map.put("supRoles","005");
        }
        if(StringUtils.isNotBlank(type)){
            //个人中心上传项目
            RecommendReason reason=recommendReasonMapper.countProject(map);
            if(reason!=null&&!"".equals(reason)){
                projectNum=reason.getProjectNum();
            }
        }else{
            //监理项目中的
            Map map1=new HashMap();
            map1.put("xmjlId",id);
            map1.put("status",status);
            RecommendReason reason=recommendReasonMapper.jlProject(map1);
            if(reason!=null&&!"".equals(reason)){
                projectNum=reason.getProjectNum();
            }
        }
        return projectNum+"";
    }
    //符合发布需求中人员要求的擅长领域的项目个数   type有值是查询监理项目否则是个人中心
    public String adetProjects(SysUser user,String type,String status){
        int projectNum=0;
        if(StringUtils.isNotBlank(user.getDoBest())) {
            RecommendReason re=new RecommendReason();
            String[] arr = user.getDoBest().split(",");
            List<String> list = new ArrayList<>();
            for (int i = 0; i < arr.length; i++) {
                list.add(arr[i]);
            }
            Map map = new HashMap<>();
            map.put("master", list);
            map.put("status",status);
            Map map1 = new HashMap<>();
            map1.put("userId", user.getId());
            map1.put("master", list);
            if("002".equals(type)){
                //总监理工程师  咨询顾问
                map1.put("supRole", "002");
                map1.put("supRoles", "007");
            }else if("003".equals(type)){
                //项目经理
                map1.put("supRole", "003");
            }else if("004".equals(type)){
                //助理   工程师
                map1.put("supRole", "004");
                map1.put("supRoles", "005");
            }
            if(StringUtils.isNotBlank(type)){
                //个人中心上传项目
                re = recommendReasonMapper.adeptZgProjects(map1);
                if(re!=null&&!"".equals(re)) {
                    projectNum = re.getProjectNum();
                }
            }else{
                //监理项目中的
                map.put("xmjlId", user.getId());
                re = recommendReasonMapper.adeptZzProjects(map);
                if(re!=null&&!"".equals(re)) {
                    projectNum = re.getProjectNum();
                }
            }
        }
        return projectNum+"";
    }

    //单个项目最大投资额  type有值是查询监理项目否则是个人中心
    public String maxMoney(String userId,String type,String status){
        Map map1=new HashMap();
        map1.put("userId",userId);
        RecommendReason supReason=new RecommendReason();
        if("002".equals(type)){
            //总监理工程师 咨询顾问
            map1.put("supRole",type);
            map1.put("supRoles","007");
        }else if("003".equals(type)){
            //项目经理
            map1.put("supRole",type);
        }else if("004".equals(type)){
            //助理、工程师   同时传入查询
            map1.put("supRole","004");
            map1.put("supRoles","005");
        }
        String project="0";
        if(StringUtils.isNotBlank(type)){
            //个人中心上传项目
            supReason=recommendReasonMapper.supMaxMoney(map1);
            if(supReason!=null&&!"".equals(supReason)){
                project=supReason.getMoneys();
            }
        }else{
            //监理项目
            Map map=new HashMap();
            map.put("status",status);
            map.put("xmjlId",userId);
            RecommendReason projectReason=recommendReasonMapper.maxMoney(map);
            if(projectReason!=null&&!"".equals(projectReason)){
                project=projectReason.getMoneys();
            }
        }
        return project;
    }

    //所有项目投资总额 type有值是查询监理项目否则是个人中心
    public String sumMoney(String userId,String type,String status){
        Map map1=new HashMap();
        map1.put("userId",userId);
        RecommendReason supReason=new RecommendReason();
        if("002".equals(type)){
            //总监理工程师  咨询顾问
            map1.put("supRole",type);
            map1.put("supRoles","007");
        }else if("003".equals(type)){
            //项目经理
            map1.put("supRole",type);
        }else if("004".equals(type)){
            //助理、工程师   同时传入查询
            map1.put("supRole","004");
            map1.put("supRoles","005");
        }
        String project="0";
        if(StringUtils.isNotBlank(type)){
            //个人中心上传项目
            supReason=recommendReasonMapper.supMaxMoney(map1);
            if(supReason!=null&&!"".equals(supReason)){
                project=supReason.getSumMoneys();
            }
        }else{
            //监理项目
            Map map=new HashMap();
            map.put("status",status);
            map.put("xmjlId",userId);
            RecommendReason projectReason=recommendReasonMapper.maxMoney(map);
            if(projectReason!=null&&!"".equals(projectReason)){
                project=projectReason.getSumMoneys();
            }
        }
        return project;
    }

    //单个项目最大服务额  type有值是查询监理项目否则是个人中心
    public String maxCharge(String userId,String type,String status){
        Map map1=new HashMap();
        map1.put("userId",userId);
        RecommendReason supReason=new RecommendReason();
        String project="0";
        if("002".equals(type)){
            //总监理工程师 咨询顾问
            map1.put("supRole",type);
            map1.put("supRoles","007");
        }else if("003".equals(type)){
            //项目经理
            map1.put("supRole",type);
        }else if("004".equals(type)){
            //助理、工程师   同时传入查询
            map1.put("supRole","004");
            map1.put("supRoles","005");
        }
        if(StringUtils.isNotBlank(type)){
            //个人中心上传项目
            supReason=recommendReasonMapper.supMaxCharge(map1);
            if(supReason!=null&&!"".equals(supReason)){
                project=supReason.getMoneys();
            }
        }else{
            //监理项目
            Map map=new HashMap();
            map.put("status",status);
            map.put("xmjlId",userId);
            RecommendReason projectReason=recommendReasonMapper.maxCharge(map);
            if(projectReason!=null&&!"".equals(projectReason)){
                project=projectReason.getMoneys();
            }

        }
        return project;
    }

    //所有项目服务总额 type有值是查询监理项目否则是个人中心
    public String sumCharge(String userId,String type,String status){
        Map map1=new HashMap();
        map1.put("userId",userId);
        RecommendReason supReason=new RecommendReason();
        String project="0";
        if("002".equals(type)){
            //总监理工程师  咨询顾问
            map1.put("supRole",type);
            map1.put("supRoles","007");
        }else if("003".equals(type)){
            //项目经理
            map1.put("supRole",type);
        }else if("004".equals(type)){
            //助理、工程师   同时传入查询
            map1.put("supRole","004");
            map1.put("supRoles","005");
        }
        if(StringUtils.isNotBlank(type)){
            //个人中心上传项目
            supReason=recommendReasonMapper.supMaxCharge(map1);
            if(supReason!=null&&!"".equals(supReason)){
                project=supReason.getSumMoneys();
            }
        }else{
            //监理项目
            Map map=new HashMap();
            map.put("status",status);
            map.put("xmjlId",userId);
            RecommendReason projectReason=recommendReasonMapper.maxCharge(map);
            if(projectReason!=null&&!"".equals(projectReason)){
                project=projectReason.getSumMoneys();
            }
        }
        return project;
    }
    //服务金额在50w及以上的项目个数     type有值是查询监理项目否则是个人中心
    public String  chargeProjects(String userId,String type,String status){
        Map map1=new HashMap();
        RecommendReason re=new RecommendReason();
        int sup=0;
        map1.put("userId",userId);
        if("002".equals(type)){
            //总监理工程师 咨询顾问
            map1.put("supRole",type);
            map1.put("supRoles","007");
        }else if("003".equals(type)){
            //项目经理
            map1.put("supRole",type);
        }else if("004".equals(type)){
            //助理、工程师   同时传入查询
            map1.put("supRole","004");
            map1.put("supRoles","005");
        }
        if(StringUtils.isNotBlank(type)){
            //查询个人中心上传项目
            re=recommendReasonMapper.supCharge(map1);
            if(re!=null&&!"".equals(re)) {
                sup = re.getProjectNum();
            }
        }else{
            //查询监理项目
            Map map=new HashMap();
            map.put("status",status);
            map.put("xmjlId",userId);
            re=recommendReasonMapper.jlHtMoney(map);
            if(re!=null&&!"".equals(re)) {
                sup = re.getProjectNum();
            }
        }
        return sup+"";
    }
    //投资金额在10000w及以上的项目个数     type有值是查询监理项目否则是个人中心
    public String  moneyProjects(String userId,String type,String status){
        Map map1=new HashMap();
        RecommendReason re=new RecommendReason();
        map1.put("userId",userId);
        int sup=0;
        if("002".equals(type)){
            //总监理工程师  咨询顾问
            map1.put("supRole",type);
            map1.put("supRoles","007");
        }else if("003".equals(type)){
            //项目经理
            map1.put("supRole",type);
        }else if("004".equals(type)){
            //助理、工程师   同时传入查询
            map1.put("supRole","004");
            map1.put("supRoles","005");
        }
        if(StringUtils.isNotBlank(type)){
            //查询个人中心上传项目
            re=recommendReasonMapper.supMoney(map1);
            if(re!=null&&!"".equals(re)) {
                sup = re.getProjectNum();
            }
        }else{
            //查询监理项目
            Map map=new HashMap();
            map.put("status",status);
            map.put("xmjlId",userId);
            re=recommendReasonMapper.jlMoney(map);
            if(re!=null&&!"".equals(re)) {
                sup = re.getProjectNum();
            }
        }
        return sup+"";
    }

    //项目工期
    public String projectDuration(String userId){
        Map map=new HashMap();
        map.put("userId",userId);
        List<SupervisorProjectDTO> list=recommendReasonMapper.selectXq(map);
        String content="";
        //按时     //延迟
        int a=0;int b=0;
        if(list!=null&&list.size()>0){
            for(SupervisorProjectDTO sup:list){
                if("003".equals(sup.getStatus())){
                    ++a;
                }else if("005".equals(sup.getStatus())){
                    ++b;
                }else if("004".equals(sup.getStatus())){
                    ++a;
                }
            }
            if(a==0&&b>0){
                content="延迟"+b+"个";
            }else if(a>0&&b==0){
                content="按时"+a+"个";
            }else if(a>0&&b>0){
                content="按时"+a+"个延迟"+b+"个";
            }
        }

        return content;
    }
    //项目工期得分 某种情况个数/总个数 * 分值 累加
    public int durationScore(String userId){
        Map map=new HashMap();
        map.put("userId",userId);
        List<SupervisorProjectDTO> list=recommendReasonMapper.selectXq(map);
        int content=0;
        //按时     //延迟
        int a=0;int b=0;
        //按时分数
        int as=Integer.parseInt(score("XMGQ001","按时"));
        //延迟分数
        int yc=Integer.parseInt(score("XMGQ001","延迟"));
        if(list!=null&&list.size()>0){
            for(SupervisorProjectDTO sup:list){
                if("003".equals(sup.getStatus())){
                    ++a;
                }else if("005".equals(sup.getStatus())){
                    ++b;
                }else if("004".equals(sup.getStatus())){
                    ++a;
                }
            }
            if((a+b)!=0) {
                content = a / (a + b) * as + b / (a + b) * yc;
            }
        }
        return content;
    }
    //是否续签
    public String renew(String userId){
        Map map=new HashMap();
        map.put("userId",userId);
        List<SupervisorProjectDTO> list=recommendReasonMapper.selectXq(map);
        String content="";
        //是     //否
        int a=0;int b=0;
        if(list!=null&&list.size()>0){
            for(SupervisorProjectDTO sup:list){
                if("001".equals(sup.getIsContinue())){
                   ++a;
                }else if("000".equals(sup.getIsContinue())){
                   ++b;
                }
            }
            if(a==0&&b>0){
                content="不续签"+b+"个";
            }else if(a>0&&b==0){
                content="续签"+a+"个";
            }else if(a>0&&b>0){
                content="续签"+a+"个不续签"+b+"个";
            }
        }
        return content;
    }
    //是否续签得分 某种情况个数/总个数 * 分值 累加
    public int renewScore(String userId){
        Map map=new HashMap();
        map.put("userId",userId);
        List<SupervisorProjectDTO> list=recommendReasonMapper.selectXq(map);
        int content=0;
        //续签     //不续签
        int a=0;int b=0;
        //续签分数
        int as=Integer.parseInt(score("SFXQ001","续签"));
        //不续签分数
        int yc=Integer.parseInt(score("SFXQ001","不续签"));
        if(list!=null&&list.size()>0){
            for(SupervisorProjectDTO sup:list){
                if("001".equals(sup.getIsContinue())){
                    ++a;
                }else if("000".equals(sup.getIsContinue())){
                    ++b;
                }
            }
            if((a+b)!=0) {
                content = a / (a + b) * as + b / (a + b) * yc;
            }
        }
        return content;
    }
    //回款及时性
     public String easeBack(String userId){
         Map map=new HashMap();
         Map mapHk=new HashMap();
         map.put("userId",userId);
         List<SupervisorProjectDTO> list=recommendReasonMapper.selectXq(map);
         String content="";
         //提前001  按时002  延期003
         int a=0;int b=0;int c=0;
         String[] arr={"003","002","001"};
         String as="";String bs="";String cs="";
         if(list!=null&&list.size()>0){
             //查询每个项目的回款数据    有一次不及时就不及时（优先级按照：不及时（即延期）>按时>提前来显示）
             for(SupervisorProjectDTO sup:list){
                 mapHk.put("projectId",sup.getId());
                 for(int i=0;i<arr.length;i++){
                     mapHk.put("type",arr[i]);
                     List<SupervisorProjectDTO> lists=recommendReasonMapper.selectHk(mapHk);
                     if(lists!=null&&lists.size()>0){
                         if ("003".equals(arr[i])) {
                             ++c;break;
                         } else if ("002".equals(arr[i])) {
                             ++b;break;
                         } else if ("001".equals(arr[i])) {
                             ++a;break;
                         }
                     }
                 }
             }
             if(a>0){
                 as="提前"+a+"个";
             }
             if(b>0){
                 bs="按时"+b+"个";
             }
             if(c>0){
                 cs="延期"+c+"个";
             }
             content=as+bs+cs;
         }

         return content;
     }
    //回款及时性得分 某种情况个数/总个数 * 分值 累加
    public int easeBackScore(String userId){
        Map map=new HashMap();
        map.put("userId",userId);
        Map mapHk=new HashMap();
        map.put("userId",userId);
        List<SupervisorProjectDTO> list=recommendReasonMapper.selectXq(map);
        int content=0;
        //提前001  按时002  延期003
        int a=0;int b=0;int c=0;
        String[] arr={"003","002","001"};
        //提前分数
        int as=Integer.parseInt(score("HKJSX001","提前"));
        //按时分数
        int yc=Integer.parseInt(score("HKJSX001","按时"));
        //延期分数
        int yq=Integer.parseInt(score("HKJSX001","延期"));
        if(list!=null&&list.size()>0){
            //查询每个项目的回款数据    有一次不及时就不及时（优先级按照：不及时（即延期）>按时>提前来显示）
            for(SupervisorProjectDTO sup:list){
                mapHk.put("projectId",sup.getId());
                for(int i=0;i<arr.length;i++){
                    mapHk.put("type",arr[i]);
                    List<SupervisorProjectDTO> lists=recommendReasonMapper.selectHk(mapHk);
                    if(lists!=null&&lists.size()>0){
                        if ("003".equals(arr[i])) {
                            ++c;break;
                        } else if ("002".equals(arr[i])) {
                            ++b;break;
                        } else if ("001".equals(arr[i])) {
                            ++a;break;
                        }
                    }
                }
            }
            if((a+b+c)!=0) {
                content = a / (a + b + c) * as + b / (a + b + c) * yc + c / (a + b + c) * yq;
            }
        }
        return content;
    }
     //束后总结及时性
    public String summarize(String userId){
        Map map=new HashMap();
        map.put("userId",userId);
        List<SupervisorProjectDTO> list=recommendReasonMapper.selectZjl(map);
        String content="";
        //及时     //不及时
        int a=0;int b=0;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if(list!=null&&list.size()>0){
                for(SupervisorProjectDTO sup:list){
                    if(StringUtils.isNotBlank(sup.getFinishDate())){
                        if(StringUtils.isNotBlank(sup.getEndPeriod())){
                           int day= DateUtil.getDayDiffer(sup.getEndPeriod(),sup.getFinishDate());
                           if(day>30){
                               ++b;
                           }else {
                               ++a;
                           }
                        }else{
                            int day= DateUtil.getDayDiffer(df.format(new Date()),sup.getFinishDate());
                            if(day>30){
                                ++b;
                            }else {
                                ++a;
                            }
                        }
                    }
                }
                if(a==0&&b>0){
                    content="不及时"+b+"个";
                }else if(a>0&&b==0){
                    content="及时"+a+"个";
                }else if(a>0&&b>0){
                    content="及时"+a+"个不及时"+b+"个";
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return content;
    }
    //束后总结及时性得分  某种情况个数/总个数 * 分值 累加
    public int summarizeScore(String userId){
        Map map=new HashMap();
        map.put("userId",userId);
        List<SupervisorProjectDTO> list=recommendReasonMapper.selectZjl(map);
        int content=0;
        //及时     //不及时
        int a=0;int b=0;
        //及时分数
        int as=Integer.parseInt(score("SHJSX001","及时"));
        //不及时分数
        int yc=Integer.parseInt(score("SHJSX001","不及时"));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if(list!=null&&list.size()>0){
                for(SupervisorProjectDTO sup:list){
                    if(StringUtils.isNotBlank(sup.getFinishDate())){
                        if(StringUtils.isNotBlank(sup.getEndPeriod())){
                            int day= DateUtil.getDayDiffer(sup.getEndPeriod(),sup.getFinishDate());
                            if(day>30){
                                ++b;
                            }else {
                                ++a;
                            }
                        }else{
                            int day= DateUtil.getDayDiffer(df.format(new Date()),sup.getFinishDate());
                            if(day>30){
                                ++b;
                            }else {
                                ++a;
                            }
                        }
                    }
                }
                if((a+b)!=0) {
                    content = a / (a + b) * as + b / (a + b) * yc;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return content;
    }
    //平台评价指标  type:评价类型
    public String evaluate(String userId,String type){
        String score="0";
        Map map=new HashMap();
        map.put("userId",userId);
        map.put("code",type);
        double sum=0.0;
        List<SupervisorProjectEvaluationDTO> eva=recommendReasonMapper.evaluate(map);
        if(eva!=null&&eva.size()>0){
            for(SupervisorProjectEvaluationDTO dto:eva) {
                sum +=Double.parseDouble(dto.getSpeLevel());
            }
            score = String.format("%.2f", (sum / eva.size()));
        }
        return score;
    }

    //相关资质
    public int competency(SysUserCertificate scf,String type){
        Map map=new HashMap();
        List<SysUserCertificate> list=sysUserCertificateMapper.select(scf);
        int number=0;
        int i=0;
        int score=0;
        if(list!=null&&list.size()>0){
            number=list.size();
            for(SysUserCertificate s:list){
                if("ZSMC001".equals(type)){
                    if(StringUtils.isNotBlank(s.getCertificateName())){
                        i=Integer.parseInt(score("ZSMCFS001",s.getCertificateName()));
                    }
                }else if("ZSJB001".equals(type)){
                    if(StringUtils.isNotBlank(s.getCertificateLevel())){
                        i=Integer.parseInt(score("ZSJB001",s.getCertificateLevel()));
                    }
                }
                score+=1/number*i;
            }
        }
        return score;
    }

    //根据金额得出对应分数
    public String moneyScore(String type,String money){
        Map map=new HashMap();
        map.put("type",type);
        List<BaseDict> list=recommendReasonMapper.score(map);
        Double a=Double.parseDouble(money);
        Double b=0.0;
        Double c=0.0;
        if(list!=null&&list.size()>0) {
            for (BaseDict base : list) {
                b=Double.parseDouble(base.getBadtLevel1());
                if(StringUtils.isNotBlank(base.getBadtLevel2())){
                    c=Double.parseDouble(base.getBadtLevel2());
                    if(a==b||a==c){
                        return base.getBadtName();
                    }else if(b<a&&a<c){
                        return base.getBadtName();
                    }
                }else{
                    if(a==b||a>b){
                        return base.getBadtName();
                    }
                }

            }
        }
        return "0";
    }
    //根据条件得出对应分数
    public String score(String type,String content){
        Map map=new HashMap();
        map.put("type",type);
        map.put("content",content);
        List<BaseDict> list=recommendReasonMapper.score(map);
        String score="0";
        if(list!=null&&list.size()>0){
            score=list.get(0).getBadtName();
        }
        return score;
    }

}
